package com.luping.xiao.xiao.controller;

import com.luping.xiao.xiao.domain.AccPassengerflowDetail;
import com.luping.xiao.xiao.service.AccPassengerflowDetailService;
import com.luping.xiao.xiao.utils.controller.BaseController;
import com.luping.xiao.xiao.utils.demoin.AjaxResult;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Controller
public class AccPassengerflowDetailController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(AccPassengerflowDetailController.class);

    @Autowired
    private AccPassengerflowDetailService accPassengerflowDetailService;


    @RequestMapping("txt")
    @ResponseBody
    public AjaxResult importDataTxt(String f) throws Exception {
        //设置列数
        final int cell = 14;
        long st = System.currentTimeMillis();
        SimpleDateFormat fa = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        SimpleDateFormat faa = new SimpleDateFormat("yy-MM-dd");
        List<AccPassengerflowDetail> list = new ArrayList<>();
        LineIterator it = null;
        BufferedWriter out = null;
        FileWriter fw = null;
        int lineNum = 0;
        int dataSize = 0;
        Pattern pattern = Pattern.compile("[0-9]*");
        try {
            it = FileUtils.lineIterator(new File(f + ".txt"));
            //写错误数据
            File fwd = new File(f + "_error.txt");
            if (!fwd.exists()) {
                fwd.createNewFile();
            }
            fw = new FileWriter(fwd);
            out = new BufferedWriter(fw);
            while (it.hasNext()) {
                lineNum++;
                //解析行数据
                String rowData = it.nextLine();
                if (rowData == null || "".equals(rowData)) {
                    continue;
                }
                log.info("lineNum=" + lineNum);
                String row[] = rowData.split("\\\",");
                if (row.length != cell) {
                    row = rowData.split(",\\\"");
                }
                if (row.length != cell) {
                    row = rowData.split(",");
                }
                if (row.length != cell) {
                    row = rowData.split("\",\"");
                }
                if (row.length != cell) {
                    log.info("异常数据行:" + lineNum);
                    out.write(rowData + "\r\n");
                    out.flush();
                    continue;
                }

                AccPassengerflowDetail  a = new AccPassengerflowDetail();
                for (int m = 0; m < row.length; m++) {
                    String val = row[m];
                    if (val != null) {
                        val = val.replaceAll("\\\"", "");
                        if (val.equals("\\N")) {
                            continue;
                        }
                        if (m==0 && !pattern.matcher(val).matches()) {
                            log.info("异常数据行:" + lineNum);
                            out.write(rowData + "\r\n");
                            out.flush();
                            break;
                        }
                        if (m == 0) {
                            a.setId(Integer.parseInt(val));
                        } else if (m == 1) {
                            a.setLineId(Integer.parseInt(val));
                        } else if (m == 2) {
                            a.setLineName(val);
                        } else if (m == 3) {
                            a.setGprsID(val);
                        } else if (m == 4) {
                            a.setDirection(Integer.parseInt(val));
                        } else if (m == 5) {
                            a.setCurStopNo(Integer.parseInt(val));
                        } else if (m == 6) {
                            a.setStationName(val);
                        } else if (m == 7) {
                            a.setOnBusCounter(Integer.parseInt(val));
                        } else if (m == 8) {
                            a.setOffBusCounter(Integer.parseInt(val));
                        } else if (m == 9) {
                            a.setTotalCounter(Integer.parseInt(val));
                        } else if (m == 10) {
                            a.setOccurTime(fa.parse(val));
                        } else if (m == 11) {
                            a.setOnBoardId(Integer.parseInt(val));
                        } else if (m == 12) {
                            a.setCarNum(val);
                        } else if (m == 13) {
                            a.setSystime(faa.parse(val));
                        }

                    }
                }
                if (a != null && a.getId()!=null) {
                    dataSize++;
                    list.add(a);
                }
                if (list.size() > 2999) {
                    saveTable(list);
                    list.clear();
                }
            }
            saveTable(list);
            long et = System.currentTimeMillis();
            long diff = et - st;
            long day = diff / (24 * 60 * 60 * 1000);
            long hour = diff / (60 * 60 * 1000) - day * 24;
            long min = diff / (60 * 1000) - day * 24 * 60 - hour * 60;
            long sec = diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
            log.info("执行完成，读取数据:" + lineNum + "行,解析数据:" + dataSize + "行，耗时:" + day + "天" + hour + "小时" + min + "分" + sec + "秒");
        } catch (Exception e) {
            log.info("异常退出");
            e.printStackTrace();
            return error();
        } finally {
            LineIterator.closeQuietly(it);
            try {
                out.flush();
                out.close();
                fw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return success();
    }

    public void saveTable(List<AccPassengerflowDetail> list) {
        accPassengerflowDetailService.insertList(list);

    }


    public static void main(String[] str) throws Exception {
        long st = System.currentTimeMillis();
        final int cell = 14;
        String pathname = "C:\\Users\\lty-045\\Desktop\\1111.txt"; // 绝对路径或相对路径都可以，写入文件时演示相对路径,读取以上路径的input.txt文件
        LineIterator it = null;
        try {
            it = FileUtils.lineIterator(new File(pathname));
            while (it.hasNext()) {
                String rowData = it.nextLine();
                //解析行数据
                if (rowData == null || "".equals(rowData)) {
                    continue;
                }
                String row[] = rowData.split("\\\",");
                if (row.length != cell) {
                    row = rowData.split(",\\\"");
                }
                if (row.length != cell) {
                    row = rowData.split(",");
                }
                if (row.length != cell) {
                    row = rowData.split("\",\"");
                }
                for (int i = 0; i < row.length; i++) {
                    System.out.println(i + "=====" + row[i]);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long et = System.currentTimeMillis();
        long diff = et - st;
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = diff / (60 * 60 * 1000) - day * 24;
        long min = diff / (60 * 1000) - day * 24 * 60 - hour * 60;
        long sec = diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
        System.out.println("行，耗时:" + day + "天" + hour + "小时" + min + "分" + sec + "秒");
    }









}
